< previous page page_674 next page >

Page 674
The following VBScript code displays the description of the type of each element in a variant array:
Dim varArray(5)
Dim i
varArray(1) = "This is a string"
varArray(2) = 123.456
varArray(3) = #12/25/1999#
varArray(4) = 10
varArray(5) = Null

For  i  = 1 to 5
  Msgbox "Array element " & i & " is of type " & TypeName(varArray(i))
Next
Notice also that the array example makes use of a For loop. In VBScript, a variable name cannot be placed after a Next statement in a For loop. Doing so causes an error.
Using Objects for Added Power
Although the VBScript language has definite limitations, keep in mind that you can use VBScript to communicate with external objects. These objects can be ActiveX DLLs that you write in regular Visual Basic or other automation objects installed on your system, such as Microsoft Excel or Word.
First, you create an instance of the object by using the CreateObject method:
Set objWord = CreateObject("Word.Application")
Next, you call methods and reference properties in the object:
objWord.Visible = True
objWord.Activate
objWord.Documents.Add
objWord.WordBasic.Insert "Here is some text"
Finally, when you are finished with the object, destroy the reference to it:
Set objWord = Nothing
note.gif In Chapter 31, "Active Server Pages," you discover Server.CreateObject(), which is used to create objects in an Active Server Page. square.gif

Learn more about creating your own custom objects in Chapter 16, "Classes: Reusable Components." After you create these objects, you can use them with the CreateObject function.
sarrow.gifSee "Creating an ActiveX DLL," p. 368
When using your own objects with VBScript, you have to handle certain return values carefully because VBScript uses only variants. Most return values such as Integer or String work as you would expect with VBScript, but arrays require special attention. Examples of returning arrays and recordsets are described in Chapter 27.

 
< previous page page_674 next page >